package com.example.logintest01;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

public class UserProfileActivity extends AppCompatActivity {

    private TextView tvNickName, tvAccount, tvAge, tvGender, tvCity, tvHome,tvSchool,tvSign,tvBirthdayTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_profile);
        Intent intent = getIntent();
        String account = intent.getStringExtra("account");

        initView();
        initData();



    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }


    private void initData() {
        getDataFromSpf();

    }

    private void getDataFromSpf() {
        SharedPreferences spfRecord = getSharedPreferences("spfRecord", MODE_PRIVATE);
        String account = spfRecord.getString("account", "");
        String nickName = spfRecord.getString("nick_name", "");
        String city = spfRecord.getString("city", "");
        String gender = spfRecord.getString("gender", "");
        String school = spfRecord.getString("school", "");
        String birthDayTime = spfRecord.getString("birth_day_time", "");
        String sign = spfRecord.getString("sign", "");

        String age = getAgeByBirthDay(birthDayTime);

        tvAccount.setText(account);
        tvNickName.setText(nickName);
        tvAge.setText(age);
        tvHome.setText(city);
        tvGender.setText(gender);
        tvSchool.setText(school);
        tvSign.setText(sign);
        tvBirthdayTime.setText(birthDayTime);



    }

    private String getAgeByBirthDay(String birthDayTime) {
        if(TextUtils.isEmpty(birthDayTime)){
            return "";
        }
        try {
            int index = birthDayTime.indexOf("年");
            String result = birthDayTime.substring(0, index);
            int parseInt = Integer.parseInt(result);
            return String.valueOf(2022 - parseInt);


        }catch(Exception e){
            e.printStackTrace();
        }

        return "";
    }

    private void initView() {
        tvAccount = findViewById(R.id.tv_account_text);
        tvNickName = findViewById(R.id.tv_nick_name);
        tvAge = findViewById(R.id.tv_age);
        tvGender = findViewById(R.id.tv_gender);
        tvHome = findViewById(R.id.tv_home_text);
        tvSchool = findViewById(R.id.tv_school_text);
        tvSign = findViewById(R.id.tv_sign_text);
        tvBirthdayTime = findViewById(R.id.tv_birth_time_text);
    }

    public void toEdit(View view) {
        Intent intent = new Intent(this, EditProfileActivity.class);
        startActivity(intent);


    }

    public void logout(View view) {

        SharedPreferences spf= getSharedPreferences("spfRecord", MODE_PRIVATE);
        SharedPreferences.Editor edit = spf.edit();
        edit.putBoolean("isLogin", false);
        edit.apply();

        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        this.finish();
    }
}